package com.sks.web.action.district;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;

import com.sks.bean.PageView;
import com.sks.bean.pojo.Building;
import com.sks.bean.pojo.Favorites;
import com.sks.bean.pojo.House;
import com.sks.bean.pojo.Users;
import com.sks.bean.uploadfile.UploadFile;
import com.sks.core.SystemConstants;
import com.sks.service.house.BuildingService;
import com.sks.service.house.FavoritesService;
import com.sks.service.house.HouseService;
import com.sks.service.privilege.UsersService;
import com.sks.utils.WebUtil;
import com.sks.web.action.BasicAction;
import com.sks.web.formbean.house.FavoritesForm;

/**
 * 小区、房源收藏功能
 * @author yuanjiayuan
 *
 */
@Controller("/sks/districtCommunity")
public class DistrictCommunity extends BasicAction{

	@Resource(name = "favoritesServiceBean") private FavoritesService favoritiesService;
	@Resource(name = "usersServiceBean") private UsersService usersService;
	@Resource(name = "buildingServiceBean") private BuildingService buildingService;
	@Resource(name = "houseServiceBean") private HouseService houseService;
	
	public ActionForward add_attentioncommunity(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		  Favorites favorites = new Favorites();
		  FavoritesForm formbean = (FavoritesForm) form;
		  List<Object> params = new ArrayList<Object>();
		  StringBuffer sb = new StringBuffer(" o.user.userId = " + WebUtil.getUsers(request).getUserId());
		  if(request.getParameter("buildingId") != null){
			  sb.append(" and o.building.buildingId = ?");
			  params.add(Integer.parseInt(request.getParameter("buildingId")));
		  }
		  else {
			  sb.append(" and o.house.houseId = ?");
			  params.add(Integer.parseInt(request.getParameter("houseId")));
		  }
	      PageView<Favorites> pv = new PageView<Favorites>(formbean.getPageSize(), 0);
	      pv.setQueryResult(favoritiesService.getScrollData(0, pv.getMaxresult(), sb.toString(), params.toArray(), null));
	      
    	  if(pv.getTotalrecord()>0){
    		  
    		  if(request.getParameter("buildingId") != null){
    			  request.setAttribute(MESSAGE, "该小区你已收藏过");
    			  request.setAttribute("urladdress", "/sks/districtFavorites.do?method=favoritesList&menuName=village");
    		  }else {
    			  request.setAttribute(MESSAGE, "该房源你已收藏过");
    			  request.setAttribute("urladdress", "/sks/districtFavorites.do?method=favoritesList&menuName=village");
    		  }
    		  
    	  }else {
    		  Users users = usersService.find(WebUtil.getUsers(request).getUserId());
    		  favorites.setUser(users);
    		  favorites.setUserId(users.getUserId());
    		  if(request.getParameter("buildingId") != null){
    			  Building building = buildingService.find(Integer.parseInt(request.getParameter("buildingId")));
    			  favorites.setBuilding(building);
    			  favorites.setBuildingId(building.getBuildingId());
    			  favorites.setBuildingName(building.getName());
        		  favorites.setF_id(null);
        		  favoritiesService.save(favorites);
        		  request.setAttribute(MESSAGE, "收藏小区成功");
        		  //request.setAttribute("urladdress", "/sks/districtFavorites.do?method=favoritesList&menuName=village");
    		  }
    		  if(request.getParameter("houseId") != null){
    			  House house = houseService.find(Integer.parseInt(request.getParameter("houseId")));
    			  favorites.setHouse(house);
    			  favorites.setHouseId(Integer.parseInt(request.getParameter("houseId")));
    			  favorites.setHouseName(house.getTitle());
        		  favorites.setF_id(null);
        		  favoritiesService.save(favorites);
        		  request.setAttribute(MESSAGE, "收藏房源成功");
        		  //request.setAttribute("urladdress", "/sks/districtFavorites.do?method=favoritesList&menuName=village");
        
    		  }
    	  }
    	  
    	StringBuffer result = new StringBuffer("[");
    	result.append("{'message':'" + request.getAttribute(MESSAGE) +"'");
    	result.append("}");
    	result.append("]");
  		response.getWriter().print(result.toString());
  		return null;
	}
	
	public ActionForward del_attentioncommunity(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Favorites favorites = new Favorites();
		FavoritesForm formbean = (FavoritesForm) form;
		List<Object> params = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer(" o.user.userId = " + WebUtil.getUsers(request).getUserId());
		if(request.getParameter("buildingId") != null){
			sb.append(" and o.building.buildingId = ?");
			params.add(Integer.parseInt(request.getParameter("buildingId")));
		}
		else {
			sb.append(" and o.house.houseId = ?");
			params.add(Integer.parseInt(request.getParameter("houseId")));
		}
		PageView<Favorites> pv = new PageView<Favorites>(formbean.getPageSize(), 0);
		pv.setQueryResult(favoritiesService.getScrollData(0, pv.getMaxresult(), sb.toString(), params.toArray(), null));
		
		if(pv.getTotalrecord()<0){
			if(request.getParameter("buildingId") != null){
				request.setAttribute(MESSAGE, "该小区已不在收藏列表");
			}else {
				request.setAttribute(MESSAGE, "该房源已不在收藏列表");
			}
		}else {
			if ( pv.getRecords().size() > 0 ) {
				favorites = pv.getRecords().iterator().next();
				favoritiesService.delete(favorites.getF_id());
			}
			if(request.getParameter("buildingId") != null){
				request.setAttribute(MESSAGE, "取消收藏小区成功");
			}
			if(request.getParameter("houseId") != null){
				request.setAttribute(MESSAGE, "取消收藏房源成功");
				
			}
		}
		
		StringBuffer result = new StringBuffer("[");
		result.append("{'message':'" + request.getAttribute(MESSAGE) +"'");
		result.append("}");
		result.append("]");
		response.getWriter().print(result.toString());
		return null;
		
	}
}
